Prepared by: Ayush Kothule
Date: July, 2021
Extracting essential data from a dataset and displaying it, is a necessary part of data science; therefore individuals can make correct decisions based on the data. In following example, we'll extract the financial information of publicly traded companies, Tesla and GameStop and analyze historical stock prices and revenue data for recommending better stock for investment based on long term growth potential.
# Install necessary packages
!pip install yfinance
!pip install bs4
!pip install html5lib
# import modules
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots
print('Modules are imported.')
Modules are imported.
In this section, we define the function make_graph. It takes a dataframe with stock data (dataframe must contain Date and Close columns), a dataframe with revenue data (dataframe must contain Date and Revenue columns), and the name of the stock.
def make_graph(stock_data, revenue_data, stock):
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
stock_data_specific = stock_data[stock_data.Date <= '2021--06-14']
revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), y=revenue_data_specific.Revenue.astype("float"), name="Revenue"), row=2, col=1)
fig.update_xaxes(title_text="Date", row=1, col=1)
fig.update_xaxes(title_text="Date", row=2, col=1)
fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
fig.update_layout(showlegend=False,
height=900,
title=stock,
xaxis_rangeslider_visible=True)
fig.show()
Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is Tesla and its ticker symbol is TSLA.
tesla = yf.Ticker("TSLA")
Using the ticker object and the function history extract stock information and save it in a dataframe named tesla_data. Set the period parameter to max so we get information for the maximum amount of time.
tesla_data = tesla.history(period='max')
Reset the index using the reset_index(inplace=True) function on the tesla_data DataFrame and display the first five rows of the tesla_data dataframe using the head function.
tesla_data.reset_index(inplace=True)
tesla_data.head(5)
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2010-06-29 | 3.800 | 5.000 | 3.508 | 4.778 | 93831500 | 0 | 0.0 |
| 1 | 2010-06-30 | 5.158 | 6.084 | 4.660 | 4.766 | 85935500 | 0 | 0.0 |
| 2 | 2010-07-01 | 5.000 | 5.184 | 4.054 | 4.392 | 41094000 | 0 | 0.0 |
| 3 | 2010-07-02 | 4.600 | 4.620 | 3.742 | 3.840 | 25699000 | 0 | 0.0 |
| 4 | 2010-07-06 | 4.000 | 4.000 | 3.166 | 3.222 | 34334500 | 0 | 0.0 |
Use the requests library to download the webpage https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue. Save the text of the response as a variable named html_data.
html_data = requests.get('https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue').text
Parse the html data using beautiful_soup.
soup = BeautifulSoup(html_data, "html.parser")
Using beautiful soup extract the table with Tesla Quarterly Revenue and store it into a dataframe named tesla_revenue. The dataframe should have columns Date and Revenue.
# find all tables in the html page. There are 6 tables in the page
html_tables = soup.find_all("table") # in html table is represented by the tag <table>
# find the table with the text 'Tesla Quarterly Revenue'
for index,table in enumerate(html_tables):
if ("Tesla Quarterly Revenue" in str(table)):
table_index = index
# create a dataframe with columns 'Date' and 'Revenue'
tesla_revenue = pd.DataFrame(columns = ['Date', 'Revenue'])
# extract the data from the table containing tesla quarterly revenue
for row in html_tables[table_index].tbody.find_all("tr"):
col = row.find_all("td")
date = col[0].text
revenue = col[1].text
tesla_revenue = tesla_revenue.append({"Date":date, "Revenue":revenue}, ignore_index=True)
tesla_revenue.head()
| Date | Revenue | |
|---|---|---|
| 0 | 2021-09-30 | $13,757 |
| 1 | 2021-06-30 | $11,958 |
| 2 | 2021-03-31 | $10,389 |
| 3 | 2020-12-31 | $10,744 |
| 4 | 2020-09-30 | $8,771 |
Execute the following line to remove the comma and dollar sign from the Revenue column.
tesla_revenue["Revenue"] = tesla_revenue['Revenue'].str.replace(',|\$',"", regex=True)
tesla_revenue.head()
| Date | Revenue | |
|---|---|---|
| 0 | 2021-09-30 | 13757 |
| 1 | 2021-06-30 | 11958 |
| 2 | 2021-03-31 | 10389 |
| 3 | 2020-12-31 | 10744 |
| 4 | 2020-09-30 | 8771 |
Execute the following lines to remove an null or empty strings in the Revenue column.
tesla_revenue.dropna(inplace=True)
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]
Display the last 5 row of the tesla_revenue dataframe using the tail function. Take a screenshot of the results.
tesla_revenue.tail(5)
| Date | Revenue | |
|---|---|---|
| 44 | 2010-09-30 | 31 |
| 45 | 2010-06-30 | 28 |
| 46 | 2010-03-31 | 21 |
| 48 | 2009-09-30 | 46 |
| 49 | 2009-06-30 | 27 |
Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is GameStop and its ticker symbol is GME.
gme = yf.Ticker('GME')
Using the ticker object and the function history extract stock information and save it in a dataframe named gme_data. Set the period parameter to max so we get information for the maximum amount of time.
gme_data = gme.history(period='max')
Reset the index using the reset_index(inplace=True) function on the gme_data DataFrame and display the first five rows of the gme_data dataframe using the head function. Take a screenshot of the results and code from the beginning of Question 3 to the results below.
gme_data.reset_index(inplace=True)
gme_data.tail(5)
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 4974 | 2021-11-15 | 203.860001 | 211.500000 | 201.690002 | 209.139999 | 1466400 | 0.0 | 0.0 |
| 4975 | 2021-11-16 | 209.020004 | 212.570007 | 203.630005 | 207.179993 | 1214600 | 0.0 | 0.0 |
| 4976 | 2021-11-17 | 206.300003 | 217.699997 | 206.000000 | 210.000000 | 1352700 | 0.0 | 0.0 |
| 4977 | 2021-11-18 | 210.250000 | 215.139999 | 207.500000 | 210.119995 | 1004400 | 0.0 | 0.0 |
| 4978 | 2021-11-19 | 209.139999 | 229.389999 | 208.250000 | 228.800003 | 3034200 | 0.0 | 0.0 |
Use the requests library to download the webpage https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue. Save the text of the response as a variable named html_data.
html_data = requests.get('https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue').text
Parse the html data using beautiful_soup.
soup = BeautifulSoup(html_data, "html.parser")
Using beautiful soup extract the table with GameStop Quarterly Revenue and store it into a dataframe named gme_revenue. The dataframe should have columns Date and Revenue. Make sure the comma and dollar sign is removed from the Revenue column using a method similar to what you did in Question 2.
# find all tables in the html page. There are 6 tables in the page
html_tables = soup.find_all("table") # in html table is represented by the tag <table>
# find the table with the text 'GameStop Quarterly Revenue'
for index,table in enumerate(html_tables):
if ("GameStop Quarterly Revenue" in str(table)):
table_index = index
# create a dataframe with columns 'Date' and 'Revenue'
gme_revenue = pd.DataFrame(columns = ['Date', 'Revenue'])
# extract the data from the table containing gamestop quarterly revenue
for row in html_tables[table_index].tbody.find_all("tr"):
col = row.find_all("td")
date = col[0].text
revenue = col[1].text
gme_revenue = gme_revenue.append({"Date":date, "Revenue":revenue}, ignore_index=True)
gme_revenue.head()
| Date | Revenue | |
|---|---|---|
| 0 | 2021-07-31 | $1,183 |
| 1 | 2021-04-30 | $1,277 |
| 2 | 2021-01-31 | $2,122 |
| 3 | 2020-10-31 | $1,005 |
| 4 | 2020-07-31 | $942 |
# remove $ and , from revenue
gme_revenue["Revenue"] = gme_revenue['Revenue'].str.replace(',|\$',"", regex=True)
Display the last five rows of the gme_revenue dataframe using the tail function. Take a screenshot of the results.
gme_revenue.tail(5)
| Date | Revenue | |
|---|---|---|
| 62 | 2006-01-31 | 1667 |
| 63 | 2005-10-31 | 534 |
| 64 | 2005-07-31 | 416 |
| 65 | 2005-04-30 | 475 |
| 66 | 2005-01-31 | 709 |
Use the make_graph function to graph the Tesla Stock Data, also provide a title for the graph. The structure to call the make_graph function is make_graph(tesla_data, tesla_revenue, 'Tesla'). Note the graph will only show data upto June 2021.
make_graph(tesla_data, tesla_revenue, 'Tesla')
Use the make_graph function to graph the GameStop Stock Data, also provide a title for the graph. The structure to call the make_graph function is make_graph(gme_data, gme_revenue, 'GameStop'). Note the graph will only show data upto June 2021.
make_graph(gme_data, gme_revenue, 'GameStop')
ℹ️ Based on the historical stock price and revenue data of Tesla and GameStop, Tesla had a steady growth compared to GameStop and is a better stock for investment.